package com.admin.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.admin.bean.CompanyBean;
import com.admin.bean.MobileProductBean;
import com.admin.bean.MobileTypeBean;
import com.admin.service.CompanyService;
import com.admin.service.MobileProductService;
import com.admin.service.MobileTypeService;
import com.admin.service.ProductService;
import com.admin.util.BaseUtil;
import com.admin.util.PageBean;

@Controller
@RequestMapping("/mobile")
public class MoblieTypeAction {

	@Autowired private CompanyService companyService;
	@Autowired private MobileTypeService mobileTypeService;
	@Autowired private ProductService productService;
	@Autowired private MobileProductService mobileProductService;
	
	@RequestMapping(value="/{companyId}")
	public String list(@PathVariable int companyId,HttpServletRequest request){
		int totalCount = mobileTypeService.findCountByCompanyId(companyId);
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<MobileTypeBean> pageBean = new PageBean<MobileTypeBean>(totalCount,currentPage);
		List<MobileTypeBean> mobileTypeList = mobileTypeService.findMobileTypeByCompanyId(companyId, pageBean.getStart(), pageBean.getPageSize());
		pageBean.setResult(mobileTypeList);
		pageBean.setUrl(String.valueOf(companyId)+"?1=1");
		request.setAttribute("pageBean", pageBean);
		
		CompanyBean company = companyService.findCompanyById(companyId);
		request.setAttribute("company", company);
		
		return "mobileList";
	}
	
	@RequestMapping("/{companyId}/new")
	public String addUI(@PathVariable int companyId,HttpServletRequest request){
		int totalCount = companyService.findCount();
		List<CompanyBean> companyList = companyService.findList(0, totalCount);
		request.setAttribute("companyList", companyList);
		
		CompanyBean company = companyService.findCompanyById(companyId);
		request.setAttribute("company", company);
		
		return "mobileAdd";
	}
	
	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String mobileTypeName = request.getParameter("mobileTypeName");
		int mobileWidth = BaseUtil.stringToInt(request.getParameter("mobileWidth"));
		int mobileHeight = BaseUtil.stringToInt(request.getParameter("mobileHeight"));
		String softwareVersion = request.getParameter("softwareVersion");
		int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
		MobileTypeBean type = new MobileTypeBean();
		type.setMobileTypeName(mobileTypeName);
		type.setMobileWidth(mobileWidth);
		type.setMobileHeight(mobileHeight);
		type.setSoftwareVersion(softwareVersion);
		type.setCompanyId(companyId);
		int result = mobileTypeService.insertMobileType(type);
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
	
	@RequestMapping("/show/{mobileTypeId}")
	public ModelAndView show(@PathVariable int mobileTypeId,HttpServletResponse response) throws IOException{
		MobileTypeBean type = mobileTypeService.findMobileTypeById(mobileTypeId);
		response.setContentType("text/html");
		response.getWriter().print(JSONObject.fromObject(type).toString());
		return null;
	}
	
	@RequestMapping("/update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int mobileTypeId = BaseUtil.stringToInt(request.getParameter("mobileTypeId"));
		String mobileTypeName = request.getParameter("mobileTypeName");
		int mobileWidth = BaseUtil.stringToInt(request.getParameter("mobileWidth"));
		int mobileHeight = BaseUtil.stringToInt(request.getParameter("mobileHeight"));
		String softwareVersion = request.getParameter("softwareVersion");
		int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
		MobileTypeBean type = new MobileTypeBean();
		type.setMobileTypeId(mobileTypeId);
		type.setMobileTypeName(mobileTypeName);
		type.setMobileWidth(mobileWidth);
		type.setMobileHeight(mobileHeight);
		type.setSoftwareVersion(softwareVersion);
		type.setCompanyId(companyId);
		int result = mobileTypeService.updateMoileType(type);
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
	
	@RequestMapping("/del/{mobileTypeId}")
	public ModelAndView del(@PathVariable int mobileTypeId,HttpServletResponse response) throws IOException{
		int result = mobileTypeService.delMobileTypeById(mobileTypeId);
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
	
	@RequestMapping("/{companyId}/{mobileTypeId}")
	public String showProductList(@PathVariable int companyId,@PathVariable int mobileTypeId,HttpServletRequest request){
		int totalCount = productService.findCount();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<MobileProductBean> pageBean = new PageBean<MobileProductBean>(totalCount,currentPage);
		List<MobileProductBean> productList = mobileProductService.findByMobileTypeId(mobileTypeId, pageBean.getStart(), pageBean.getPageSize());
		pageBean.setResult(productList);
		pageBean.setUrl(String.valueOf(mobileTypeId)+"?1=1");
		request.setAttribute("pageBean", pageBean);
		
		CompanyBean company = companyService.findCompanyById(companyId);
		request.setAttribute("company", company);
		
		MobileTypeBean mobileType = mobileTypeService.findMobileTypeById(mobileTypeId);
		request.setAttribute("mobileType", mobileType);
		
		return "mobileProductList";
	}
	
	@RequestMapping("/addbatch/{mobileTypeId}")
	public ModelAndView insertProduct(@PathVariable int mobileTypeId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String[] productIds = request.getParameterValues("productId");
		int[] result = mobileProductService.insertBatch(mobileTypeId, productIds);
		response.setContentType("text/html");
		if(productIds.length==result.length){
			response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}
		return null;
	}
	
	@RequestMapping("/delbatch/{mobileTypeId}")
	public ModelAndView delProduct(@PathVariable int mobileTypeId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String[] ids = request.getParameterValues("id");
		int[] result = mobileProductService.delBatch(ids);
		response.setContentType("text/html");
		if(ids.length==result.length){
			response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}
		return null;
	}
}
